﻿using System;
using System.Linq;
using System.Collections.Generic;
using AdventureWorksImplementation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1 : IDisposable
    {

        private readonly EntityService _entityService;

        public UnitTest1()
        {
            _entityService = new EntityService();
        }

        [TestMethod]
        ///select p.ProductID, p.Name, p.ProductNumber, wo.WorkOrderID
        // from Production.WorkOrder wo
        //   inner join Production.Product p on p.ProductID = wo.ProductID
        // where p.ProductID = 744
        public void WorkOrdersRelatedToProduct()
        {
            var result = _entityService.All<WorkOrder>( 744 ).ToList();
            Assert.AreEqual( 69, result.Count );
            Assert.IsTrue( result.Any( wo => wo.WorkOrderID == 63 ) );
        }

        [TestMethod]
        /// select pr.*
        //  from Production.ProductReview pr
        //  	inner join Production.Product p on pr.ProductID = p.ProductID
        //  	inner join Production.ProductInventory pri on p.ProductID = pri.ProductID
        //  where pri.LocationID = 50
        //  	and p.ProductID = 937
        public void ProductReviewsForProduct_Location()
        {
            var result = _entityService.All<ProductReview>( 937, null, 50 ).ToList();
            Assert.AreEqual( 2, result.Count );
            Assert.IsTrue( result.Any( pr => pr.ProductReviewID == 2 ) );
        }

        [TestMethod]
        /// select wo.* from
        //	  Production.WorkOrder wo
        //	  inner join Production.Product p on wo.ProductID = p.ProductID
        //	  inner join Production.ProductInventory pin on p.ProductID = pin.ProductID
        //	  inner join Production.Location l on pin.LocationID = l.LocationID
        //  where l.LocationID = 10
        //	  and p.ProductID = 747
        //	  and wo.OrderQty > 75
        public void WorkOrdersByProduct_Location()
        {
            var qry = _entityService.All<WorkOrder>( 747, null, 10 );
            var result = ( from wo in qry where wo.OrderQty > 75 select wo ).ToList();
            Assert.AreEqual( 18, result.Count );
            Assert.IsTrue( result.Any( wo => wo.WorkOrderID == 5016 ) );
        }

        [TestMethod]
        /// select pd.* from
        //	Production.Product p 
        //	  inner join Production.ProductDocument pd on p.ProductID = pd.ProductID
        //	  inner join Production.WorkOrder wo on p.ProductID = wo.ProductID
        //  where p.ProductID = 514
        //	  and wo.WorkOrderID = 36651
        public void ProductDocumentByProduct_WorkOrder()
        {
            var result = _entityService.All<ProductDocument>( 514, new List<int> { 36651 } ).ToList();
            Assert.AreEqual( 1, result.Count );
        }

        public void Dispose()
        {
            Dispose( true );
            GC.SuppressFinalize( this );
        }

        protected virtual void Dispose( bool disposing )
        {
            if ( disposing )
                if ( _entityService != null )
                    _entityService.Dispose();
        }

        ~UnitTest1()
        {
            Dispose( false );
        }

    }
}
